package com.zh.note.huawei.onehundred.好朋友;

import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

/**
 * @Author: zhanghuan
 * @date: 2023/9/3 10:06
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // int n = sc.nextInt();
        // String line = sc.nextLine();
        int n = 9;
        String line = "123 124 125 121 119 122 126 123 100";
        String[] s = line.split(" ");
        int[] nums = Arrays.stream(s).mapToInt(Integer::parseInt).toArray();
        int[] res = new int[n];
        // 栈存储下标，维护一个单调递减的栈
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < nums.length; i++) {
            while (!stack.isEmpty() && nums[i] > nums[stack.peek()]) {
                int index = stack.pop();
                res[index] = i;
            }
            stack.push(i);
        }
        System.out.println(Arrays.toString(res).replaceAll("[\\[\\],]", ""));
    }
}
